#!/bin/bash

# 成功校验
function check(){
    if [ $? -ne 0 ];then
        exit $1
    fi
}

tmp=$1
tag=${tmp// /}
git_version=$(git tag -n --sort=-taggerdate | grep $tag | head -1)
times_num=$2

git checkout -b $git_version

image_version=${git_version// /}
version="$image_version-$times_num"
# 构建版本号
echo "构建版本："$version
echo "标签版本："$tag
echo "镜像版本："$image_version

# 编译go程序
export CGO_ENABLED="0"   
export GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn
go mod init go-dingding
go mod tidy
go build
echo "编译打包完成"

# go-dingding应用部署更新脚本
echo '#!/bin/bash' > deploy-go-dingding.sh
echo "source /etc/profile" >> deploy-go-dingding.sh
echo "ps -ef |grep go-dingding | awk 'NR==1' | awk '{print $2}' | xargs kill" >> deploy-go-dingding.sh
echo "chmod +x /app/go-dingding" >> deploy-go-dingding.sh
echo "nohup /app/go-dingding > log.file  2>&1 & " >> deploy-go-dingding.sh

